<?php
namespace App\Console\Commands;


use App\Models\Merchant;
use App\Models\MerchantWalletDayCount;
use App\Models\MerchantWalletDetail;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class Settlement extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'settlement';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'settlement';

    /**
     * Settlement constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            //新大陆
            $source_type = '8000';
            $time = date("Y-m-d", time());
            $MerchantWalletDayCount = MerchantWalletDayCount::where('day', '<', $time)
                ->where('source_type', $source_type)
                ->where('settlement', '02')
                ->select('store_id', 'merchant_id', 'money', 'day', 'id')
                ->first();
            if ($MerchantWalletDayCount) {
                $merchant_id = $MerchantWalletDayCount->merchant_id;
                $money = $MerchantWalletDayCount->money;
                $day = $MerchantWalletDayCount->day;
                $s_time = date("Y-m-d 00:00:00", strtotime($day));
                $e_time = date("Y-m-d 23:59:59", strtotime($day));
                $id = $MerchantWalletDayCount->id;

                //开启事务
                try {
                    DB::beginTransaction();
                    //总表结算
                    MerchantWalletDayCount::where('id', $id)->update([
                        'settlement' => '01',
                    ]);

                    $table = 'merchant_wallet_details' . '_' . $day;
                    if (Schema::hasTable($table)) {
                        $merchant_wallet_details = DB::table($table);
                    } else {
                        $merchant_wallet_details = DB::table('merchant_wallet_details');
                    }

                    //明细结算
                    $merchant_wallet_details->where('source_type', $source_type)
                        ->where('merchant_id', $merchant_id)
                        ->where('created_at', '>=', $s_time)
                        ->where('created_at', '<=', $e_time)
                        ->where('settlement', '02')
                        ->update([
                            'settlement' => '01',
                            'settlement_desc' => "已结算",
                        ]);

                    //用户金额结算
                    $Merchant = Merchant::where('id', $merchant_id)
                        ->first();
                    if ($Merchant) {
                        $Merchant->money = $Merchant->money + $money;
                        $Merchant->save();
                    }

                    DB::commit();
                } catch (\Exception $e) {
                    Log::info($e->getMessage().' | '.$e->getFile().' | '.$e->getLine());
                    DB::rollBack();
                    return json_encode([
                        'status' => '2',
                        'message' => $e->getMessage().' | '.$e->getFile().' | '.$e->getLine()
                    ]);
                }
            }
        } catch (\Exception $exception) {
            Log::info('新大陆商户结算定时任务error');
            Log::info($exception->getMessage().' | '.$exception->getFile().' | '.$exception->getLine());
        }
    }


}
